package com.todo.event.activity.infrastructure.convert;

import com.google.api.client.util.Lists;
import com.todo.common.utils.TodoCommonUtils;
import com.todo.event.activity.infrastructure.entity.domain.EventDetailDO;
import com.todo.event.activity.infrastructure.entity.po.EventFieldInfoPO;
import com.todo.event.activity.infrastructure.entity.po.EventInfoPO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/1/7 12:05
 * @description
 */
public class EventActivityConvert {

    public static EventInfoPO buildEventInfoPO(EventDetailDO eventDetailDO) {
        if (eventDetailDO == null || eventDetailDO.getEventInfo() == null) {
            return null;
        }
        EventInfoPO eventInfoPO = new EventInfoPO();
        eventInfoPO.setId(eventDetailDO.getId());
        eventInfoPO.setEventCode(eventDetailDO.getEventCode());
        eventInfoPO.setBusinessLine(eventDetailDO.getBusinessLine());
        eventInfoPO.setCreateUser(eventDetailDO.getCreateUser());
        eventInfoPO.setLastUpdateUser(eventDetailDO.getLastUpdateUser());
        eventInfoPO.setStatus(eventDetailDO.getStatus());
        eventInfoPO.setIsDelete(eventDetailDO.getIsDelete());
        eventInfoPO.setCreateTime(eventDetailDO.getCreateTime());
        eventInfoPO.setUpdateTime(eventDetailDO.getUpdateTime());
        EventDetailDO.EventInfo eventInfo = eventDetailDO.getEventInfo();
        eventInfoPO.setEventClassify(eventInfo.getEventClassify());
        eventInfoPO.setEventType(eventInfo.getEventType());
        eventInfoPO.setEventName(eventInfo.getEventName());
        eventInfoPO.setTopicName(eventInfo.getTopicName());
        eventInfoPO.setTag(eventInfo.getTag());
        eventInfoPO.setConsumerName(eventInfo.getConsumerName());
        eventInfoPO.setQueueType(eventInfo.getQueueType());
        eventInfoPO.setBaseConf(eventInfo.getBaseConf());
        eventInfoPO.setExtraConf(eventInfo.getExtraConf());
        eventInfoPO.setRule(eventInfo.getRule());
        return eventInfoPO;
    }

    public static List<EventFieldInfoPO> buildEventFieldInfoPOList(EventDetailDO eventDetailDO) {
        if (eventDetailDO == null || CollectionUtils.isEmpty(eventDetailDO.getEventFieldList())) {
            return Lists.newArrayList();
        }
        List<EventFieldInfoPO> result = new ArrayList<>();
        for (EventDetailDO.EventField eventField : eventDetailDO.getEventFieldList()) {
            EventFieldInfoPO eventFieldInfoPO = new EventFieldInfoPO();
            eventFieldInfoPO.setId(TodoCommonUtils.buildLongGuid());
            eventFieldInfoPO.setEventCode(eventDetailDO.getEventCode());
            eventFieldInfoPO.setDataSource(eventField.getDataSource());
            eventFieldInfoPO.setTransfer(eventField.getTransfer());
            eventFieldInfoPO.setFieldName(eventField.getFieldName());
            eventFieldInfoPO.setFieldType(eventField.getFieldType());
            eventFieldInfoPO.setFieldLength(eventField.getFieldLength());
            eventFieldInfoPO.setFieldDesc(eventField.getFieldDesc());
            eventFieldInfoPO.setUniqueField(eventField.getUniqueField());
            eventFieldInfoPO.setOutput(eventField.getOutput());
            eventFieldInfoPO.setSelectType(eventField.getSelectType());
            eventFieldInfoPO.setSelectOperate(eventField.getSelectOperate());
            eventFieldInfoPO.setEnumsValue(eventField.getEnumsValue());
            eventFieldInfoPO.setIsDelete(0);
            eventFieldInfoPO.setCreateTime(new Date());
            eventFieldInfoPO.setUpdateTime(new Date());
            result.add(eventFieldInfoPO);
        }
        return result;
    }

    public static EventDetailDO buildEventDetailDO(EventInfoPO eventInfoPO, List<EventFieldInfoPO> eventFieldInfoPOS) {
        if (eventInfoPO == null || CollectionUtils.isEmpty(eventFieldInfoPOS)) {
            return null;
        }
        EventDetailDO eventDetailDO = new EventDetailDO();
        eventDetailDO.setId(eventInfoPO.getId());
        eventDetailDO.setBusinessLine(eventInfoPO.getBusinessLine());
        eventDetailDO.setEventCode(eventInfoPO.getEventCode());
        eventDetailDO.setCreateUser(eventInfoPO.getCreateUser());
        eventDetailDO.setLastUpdateUser(eventInfoPO.getLastUpdateUser());
        eventDetailDO.setStatus(eventInfoPO.getStatus());
        eventDetailDO.setIsDelete(eventInfoPO.getIsDelete());
        eventDetailDO.setCreateTime(eventInfoPO.getCreateTime());
        eventDetailDO.setUpdateTime(eventInfoPO.getUpdateTime());

        EventDetailDO.EventInfo eventInfo = new EventDetailDO.EventInfo();
        eventInfo.setEventClassify(eventInfoPO.getEventClassify());
        eventInfo.setEventType(eventInfoPO.getEventType());
        eventInfo.setEventName(eventInfoPO.getEventName());
        eventInfo.setTopicName(eventInfoPO.getTopicName());
        eventInfo.setTag(eventInfoPO.getTag());
        eventInfo.setConsumerName(eventInfoPO.getConsumerName());
        eventInfo.setQueueType(eventInfoPO.getQueueType());
        eventInfo.setBaseConf(eventInfoPO.getBaseConf());
        eventInfo.setExtraConf(eventInfoPO.getExtraConf());
        eventInfo.setRule(eventInfoPO.getRule());
        eventDetailDO.setEventInfo(eventInfo);

        List<EventDetailDO.EventField> eventFieldList = new ArrayList<>();
        for (EventFieldInfoPO eventFieldInfoPO : eventFieldInfoPOS) {
            EventDetailDO.EventField eventField = new EventDetailDO.EventField();
            eventField.setDataSource(eventFieldInfoPO.getDataSource());
            eventField.setTransfer(eventFieldInfoPO.getTransfer());
            eventField.setFieldName(eventFieldInfoPO.getFieldName());
            eventField.setFieldType(eventFieldInfoPO.getFieldType());
            eventField.setFieldLength(eventFieldInfoPO.getFieldLength());
            eventField.setFieldDesc(eventFieldInfoPO.getFieldDesc());
            eventField.setUniqueField(eventFieldInfoPO.getUniqueField());
            eventField.setOutput(eventFieldInfoPO.getOutput());
            eventField.setSelectType(eventFieldInfoPO.getSelectType());
            eventField.setSelectOperate(eventFieldInfoPO.getSelectOperate());
            eventField.setEnumsValue(eventFieldInfoPO.getEnumsValue());
            eventFieldList.add(eventField);
        }
        eventDetailDO.setEventFieldList(eventFieldList);
        return eventDetailDO;
    }
}
